package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.SetmealServiceImpl;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealServiceImpl setmealService;

    @GetMapping("/page")
    public Result<PageResult> getAllSetMeals(SetmealPageQueryDTO setmealPageQueryDTO){
        return Result.success(setmealService.getAllSetMeals(setmealPageQueryDTO));
    }

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<Object> setSetmealStatus(@RequestParam Long id, @PathVariable Integer status){
        setmealService.setSetmealStatus(id,status);
        return Result.success();
    }

    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", key = "#SetmealDTO.categoryId")
    public Result<Object> addNewSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.addNewSetmeal(setmealDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id){
        return Result.success(setmealService.getSetmealById(id));
    }

    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> deleteSetmeal(@RequestParam List<Long> ids){
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }
}
